package com.jimet.process;

import java.lang.reflect.Method;

public class ListActionProcessorBase {

	protected Object myObject;
	private Class objClass;
	
	public ListActionProcessorBase( String objectName  ) throws Exception {

		Thread t = Thread.currentThread();
		ClassLoader cl = t.getContextClassLoader();
		
		try {
			objClass  					= cl.loadClass(objectName);
			myObject  					= objClass.newInstance();
		} 
		catch (Exception e) {
			throw new Exception( "Invalid object definition for obj instance : ["+ objectName + "]");
		}
	}
	
	public String doProcess ( String strAction , String strParams , Object paramObj ) throws Exception
	{
		String ret		= "";
		try {
			Class[] paramsClasses = new Class[3];
			paramsClasses[0] = String.class;
			paramsClasses[1] = String.class;
			paramsClasses[2] = Object.class;
			
			Method doProcessMethod  	= objClass.getMethod("doProcess", paramsClasses);
			
			Object [] paramsArr			= new Object[3];
			paramsArr[0]				= strAction;
			paramsArr[1]				= strParams;
			paramsArr[2]				= paramObj;
			ret							= String.valueOf( doProcessMethod.invoke(myObject,	paramsArr));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return ret;
	}
	
	public boolean doValidate ( String strAction , String strParams ) throws Exception
	{
		boolean ret		= false;
		try {
			Class[] paramsClasses = new Class[2];
			paramsClasses[0] = String.class;
			paramsClasses[1] = String.class;
			
			Method doValidateMethod  	= objClass.getMethod("doValidate", paramsClasses);
			
			Object [] paramsArr			= new Object[2];
			paramsArr[0]				= strAction;
			paramsArr[1]				= strParams;
			Object o 					= doValidateMethod.invoke(myObject,paramsArr);		
			ret							= (Boolean)o;
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
		return ret;
	}
}
